#!/usr/bin/env python
# coding: utf-8

'''
handler中获取请求参数的方法
'''
import tornado.web
import tornado.ioloop
import tornado.httpserver
from tornado.options import options, define, parse_command_line
from tornado.web import RequestHandler, MissingArgumentError, url

define("port", default=8000, type=int, help='run server on the given port.')

class IndexHandler(RequestHandler):
    '''
    get_body_argument: 只能获取form-data和multi-part类型的字段，不支持json body
    get_argument: 同时支持get和post
    '''
    def post(self):
        # body_a = self.get_body_argument('a', default='post-a')   #POST / , return post-a
        # body_a = self.get_body_argument('a')   #POST / a=fuck&b=fuckyou, return fuck
        body_alist = self.get_body_arguments('a') #POST / a=cow&a=pig, return ['cow', 'pig']
        print(body_alist)
    def get(self):
        # a = self.get_query_argument('a')  #/?a=1, return 1
        # a = self.get_query_argument('a',default=0,strip=True)  #/, return 0
        # a = self.get_query_argument('a')  #/?a=1&a=2&a=3, return 3
        alist = self.get_query_arguments('a') #/?a=1&a=2&a=3, return ['1', '2', '3']
        print(alist)


if __name__ == '__main__':
    parse_command_line()
    app = tornado.web.Application([
        url(r'/', IndexHandler, name='index'),
    ], debug=True)
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
